package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.PurchaseInventory;
import com.yc.supermarket.service.PurchaseInventoryService;
import com.yc.supermarket.service.impl.PurchaseInventoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/purchaseInventory")
public class PurchaseInventoryController extends BaseController {

    private PurchaseInventoryService service = new PurchaseInventoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op=req.getParameter("op");
        if("findByTrem".equals(op)){
            doFindByTrem(req,resp);
        }else if("add".equals(op)){
            doAdd(req,resp);
        }else if("inventory".equals(op)){
            doInventory(req,resp);
        }else {
            printToJson(resp,"No method.....405");
        }
    }

    protected void doFindByTrem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PurchaseInventory pur = parseRequest(req,PurchaseInventory.class);
        Map<String,Object> map = service.findByTrem(pur);
        System.out.println(map.get("data"));
        printToJson(resp,map);
    }

    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PurchaseInventory pur = parseRequest(req,PurchaseInventory.class);
        Boolean result = service.add(pur);
        printToJson(resp,result);
    }

    protected void doInventory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PurchaseInventory pur = parseRequest(req,PurchaseInventory.class);
        Boolean result = service.inventory(pur);
        printToJson(resp,result);
    }

}
